/*绘制饼图*/

var onit=true;
var num=0;
var stat_array = new Array();
var Stop = new Array();

/*饼块上移*/
function moveup(iteam,top,txt,rec)
{
    temp = eval(iteam);
    tempat = eval(top);
    temptxt = eval(txt);
    temprec = eval(rec);
    at = parseInt(temp.style.top);
    temprec.style.display = ""; 
    if (num > 27)
    {
        temptxt.style.display = "";
    }
    if(at>(tempat-28) && onit)
    {
        num++;
        temp.style.top = at-1;
        Stop[num]=setTimeout("moveup(temp,tempat,temptxt,temprec)",10);
    }
    else
    {
        return;
    } 
}

var testFlag = false;
function alertTest()
{
    testFlag = !testFlag;
    alert(num);
}

/*饼块归位*/            
function movedown(iteam,top,txt,rec)
{
    temp = eval(iteam);
    temptxt = eval(txt);
    temprec = eval(rec);
    for(i = 0; i < Stop.length; i++)
    {
        clearTimeout(Stop[i]);
        Stop[i] = -1;
	}
    temp.style.top = top;
    num = 0;
    temptxt.style.display = "none";
    temprec.style.display = "none";   
}
            
function ontxt(iteam,top,txt,rec)
{
    temp = eval(iteam);
    temptxt = eval(txt);
    temprec = eval(rec);
    if (onit)
    {
        temp.style.top = top-28;
        temptxt.style.display = "";
        temprec.style.display = "";
    }  
}
            
function movereset(over)
{
    if (over==1)
    {
        onit=false;
    }
    else
    {
        onit=true;
    }
}


/*绘制单个饼图*/
/*num ：饼块个数*/
/*table_left:饼图左边界*/
/*table_top:饼图顶边界*/
/*all_width:饼图宽度*/
/*all_height:饼图高度*/
/*table_title:饼图标题*/
/*unit:数据度量单位*/
function table(num,table_left,table_top,all_width,all_height,table_title,unit,pieCount)
{
    var allvalues=0;
    var color = new Array();
    var bg_color = new Array(num);
    var pie = new Array(num);
    
    var tmPieID = "_" + pieCount;

	color[0]="#ff8c19";
	color[1]="#ff1919";
	color[2]="#ffff19";
	color[3]="#1919ff";
	color[4]="#19ff19";
	color[5]="#fc0000";
	color[6]="#3cc000";
	color[7]="#ff19ff";
	color[8]="#993300";
	color[9]="#f60000";
    
    color[10]="#DC143C";
    color[11]="#00008B";
    color[12]="#008B8B";
    color[13]="#B8860B";
    color[14]="#A9A9A9";
    color[15]="#006400";

    for (i=0,j=0; i<num; i++,j++)
    {
        bg_color[i] = color[j];
        if (j == color.length)
        {
            j = -1;
        }
    }               
    
    for (i=0; i<num; i++)
    {
        allvalues += stat_array[0][i];
        
    }
    
    //如果所有的数值和为0 则返回，不绘制饼图
    if(allvalues ==0) return "";
    
    var k = 0;
    for (i=0; i<num; i++)
    {                   
        //这里只保留两位小数，即xx.xx%
        var tmFloat = 0;
        if(stat_array[0][i] > 0)
        {
            tmFloat = parseInt((stat_array[0][i])/allvalues*10000)/100;                  
        }
        else
        {
            tmFloat = 0;
        }
        if(tmFloat == 0)
        {
            pie[i] = 0;
        }
        else
        {
            pie[i] = parseFloat(tmFloat).toFixed(2);
        }
    }   
          
    var coordx=1000;
    var coordy=1000;  
    var zoomRate=1;
        
    var result="";
    //改px result += "<v:group style='width:350;height:350;position:relative;top:0' CoordSize='400,400'>\n";
    result += "<v:group style='width:350px;height:350px;position:relative;top:0' CoordSize='400,400'>\n";  
              
    result += "<v:shapetype id='Cake_3D' coordsize='21600,21600' o:spt='95' adj='11796480,5400' path='al10800,10800@0@0@2@14,10800,10800,10800,10800@3@15xe'></v:shapetype>";
    
    result += "<v:shapetype id='3dtxt' coordsize='21600,21600' o:spt='136' adj='10800' path='m@7,l@8,m@5,21600l@6,21600e'>";
    result += "<v:path textpathok='t' o:connecttype='custom' o:connectlocs='@9,0;@10,10800;@11,21600;@12,10800' o:connectangles='270,180,90,0'/>";
    result += "<v:textpath on='t' fitshape='t'/>";
    result += "<o:lock v:ext='edit' text='t' shapetype='t'/>";
    result += "</v:shapetype>";
    
   
    result += "<v:group ID='table' style='position:absolute;left:" + table_left + "px;top:" + table_top + "px;WIDTH:" + all_width + "px;HEIGHT:" + all_height + "px;' coordsize = '21000,11500'>";
    
    //饼图标题
    //改px result += "<v:Rect style='position:relative;left:400;top:0;width:20000;height:800' filled='false' stroked='false'>";
    result += "<v:Rect style='position:relative;left:400px;top:0;width:20000px;height:800px' filled='false' stroked='false'>";
   
    result += "<v:TextBox inset='0pt,0pt,0pt,0pt'>";
    result += "<table width='100%' border='0' align='center' cellspacing='0'>";
    result += "<tr>";
    result += "<td align='center' valign='middle'><div style='font-size:10pt; font-family:宋体;'><B>" + table_title + "  " +chartValueUnit + "</B></div></td>";
    result += "</tr>";
    result += "</table>";
    result += "</v:TextBox>";
    result += "</v:Rect> ";
    
    //图例外围矩形宽度
    var tmLengendWidth = 100;
  
    //单个字体宽度
    var tmLengFontWid = (22) * 21600 /1000;                
  
    //获取图例最大宽度
    for(j = 0; j < num; j++)
    {
       if(("" + stat_array[1][j]).length*tmLengFontWid > tmLengendWidth)
       {
            tmLengendWidth = 1300 + ("" + stat_array[1][j]).length * tmLengFontWid;
       }
    }  
    
    
    tmLengendWidth=3500;//by xj
    //图例背景框
    //改px result += "<v:rect id='back' style='position:relative;left:15000;top:200;width:" + tmLengendWidth + "; height:" + ((num+1)*9000/14+400) + ";' fillcolor='#9cf' stroked='t' strokecolor='#0099ff'>";
    result += "<v:rect id='back' style='position:relative;left:15000px;top:200px;width:" + tmLengendWidth + "px; height:" + ((num+1)*9000/14+400) + "px;' fillcolor='#9cf' stroked='t' strokecolor='#0099ff'>";
   
    result += "<v:fill rotate='t' angle='-175' focus='100%' type='gradient'/>";
    result += "<v:shadow on='t' type='single' color='silver' offset='3pt,3pt'/>";
    result += "</v:rect>";
    
    //图例二字
    var tmLeft = 15000 + (tmLengendWidth / 2 - 4 * tmLengFontWid);
   //改px result += "<v:Rect style='position:relative;left:" + tmLeft + ";top:200;width:4000;height:1700' filled='f' stroked='f'>";
    result += "<v:Rect style='position:relative;left:" + tmLeft + "px;top:200px;width:4000px;height:1700px' filled='f' stroked='f'>";
    
    result += "<v:TextBox inset='8pt,2pt,3pt,3pt' style='font-size:8pt;'><div align='left'><B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;图例</B></div></v:TextBox>";
    result += "</v:Rect>" ;
    
    var tmRectWid = tmLengendWidth - 600;
    
    //图例中每个元素
    for (i=0; i<num; i++)
    {
        //浮动的小框
        //改px result += "<v:Rect id='rec" + i + tmPieID + "' style='position:relative;left:15400;top:" + Math.round((i+1)*9000/14 + 380) + ";width:"+ tmRectWid + ";height:590;display:none' fillcolor='#efefef' strokecolor='" + bg_color[i] + "'>";
         result += "<v:Rect id='rec" + i + tmPieID + "' style='position:relative;left:15400px;top:" + Math.round((i+1)*9000/14 + 380) + "px;width:"+ tmRectWid + "px;height:590px;display:none' fillcolor='#efefef' strokecolor='" + bg_color[i] + "'>";
       
        result += "<v:fill opacity='.6' color2='fill darken(118)' o:opacity2='.6' rotate='t' method='linear sigma' focus='100%' type='gradient'/>";
        result += "</v:Rect>";      
        //小色块
        //改px result += "<v:Rect style='position:relative;left:15500;top:" + Math.round((i+1)*9000/14 + 500) + ";width:300;height:400' fillcolor='" + bg_color[i] + "' stroked='f'/>";   
         result += "<v:Rect style='position:relative;left:15500px;top:" + Math.round((i+1)*9000/14 + 500) + "px;width:300px;height:400px' fillcolor='" + bg_color[i] + "' stroked='f'/>";   
        //色块后的文字
        //改px result += "<v:Rect style='position:relative;left:16300;top:" + Math.round((i+1)*9000/14 + 500) + ";width:" + tmRectWid+ ";height:500' filled='f' stroked='f' fillcolor='" +bg_color[i] + ">";
        result += "<v:Rect style='position:relative;left:16300px;top:" + Math.round((i+1)*9000/14 + 500) + "px;width:" + tmRectWid+ "px;height:500px' filled='f' stroked='f' fillcolor='" +bg_color[i] + ">";
        
        result += "<v:TextBox inset='2pt,2pt,2pt,2pt' style='POSITION:absolute;font-size:8pt;Z-INDEX:10;'><div align='left'>" + stat_array[1][i] + "</div></v:TextBox>";
        result += "</v:Rect> " ;
    } 
    result += "</v:group>";
    
    var k1 = 180;
    var k4 = 10;
    
    //绘制每个饼块
    for (i=0; i<num; i++)
    {
        k2 = 360 * pie[i]/100/2;
        k3 = k1 + k2;
        if (k3 >= 360)
        {
            k3 = k3 - 360;
        }
        kkk = (-11796480 * pie[i]/100 + 5898240);
        k5 = 3.1414926 * 2 * (180-(k3-180))/360;
        R = all_height/2;
        txt_x = table_left + all_height/8-30 + R + R * Math.sin(k5) * 0.7;
        txt_y = table_top + all_height/14-39 + R + R * Math.cos(k5) * 0.7 * 0.5;      
        titlestr = "&nbsp;名&nbsp;&nbsp;称：" + stat_array[1][i] + "&#13;&#10;&nbsp;数&nbsp;&nbsp;值：" + stat_array[0][i] + unit + "&#13;&#10;&nbsp;所占比例：" +  pie[i] + "%&nbsp;&nbsp;" 
        
        //饼块
        result += "<div style='cursor:hand;'>";
        result += "<v:shape id='cake" + i +  tmPieID + "' type='#Cake_3D' title='" + titlestr + "'";
        result += "style='position:absolute;left:" + table_left+all_height/8 + "px;top:" + table_top+all_height/16  + "px;WIDTH:" + all_height + "px;HEIGHT:" + all_height + "px;rotation:" + k3 + ";z-index:" + k4 + "'";
        result += "adj='" + kkk + ",0' fillcolor='" + bg_color[i] + "' onmouseover='moveup(cake" + i + tmPieID + "," + (table_top+all_height/14) + ",txt" + i + tmPieID + ",rec" + i + tmPieID + ")'; onmouseout='movedown(cake" + i + tmPieID + "," + (table_top+all_height/14) + ",txt" + i + tmPieID + ",rec" + i + tmPieID + ");'>";
        result += "<v:fill opacity='60293f' color2='fill lighten(120)' o:opacity2='60293f' rotate='t' angle='-135' method='linear sigma' focus='100%' type='gradient'/>";
        result += "<o:extrusion v:ext='view' on='t'backdepth='25' rotationangle='60' viewpoint='0,0'viewpointorigin='0,0' skewamt='0' lightposition='-50000,-50000' lightposition2='50000'/>";
        result += "</v:shape>";
        
        //三维字体
        result += "<v:shape id='txt" + i + tmPieID + "' type='#3dtxt' style='position:absolute;left:" + txt_x + "px;top:" + txt_y + "px;z-index:20;display:none;width:50; height:18;' fillcolor='#ffffff'";
        result += "onmouseover='ontxt(cake" + i + tmPieID + "," + (table_top+all_height/14) + ",txt" + i + tmPieID + ",rec" + i + tmPieID + ")'>";
        result += "<v:fill opacity='60293f' color2='fill lighten(120)' o:opacity2='60293f' rotate='t' angle='-135' method='linear sigma' focus='100%' type='gradient'/>";
        result += "<v:textpath style='font-family:'宋体';v-text-kern:t' trim='t' fitpath='t' string='" + pie[i] + "%'/>";
        result += "<o:extrusion v:ext='view' backdepth='8pt' on='t' lightposition='0,0' lightposition2='0,0'/>";
        result += "</v:shape>";       
        result += "</div>";
                    
        k1 = k1+k2*2;
        if (k1 >= 360)
        {
            k1 = k1-360;
        }
        if (k1 > 180)
        {
            k4 = k4+1;
        }
        else
        {
            k4 = k4-1;
        }
      }
          result +="<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></v:group>";
          
          return result;
}

/*绘制单个饼图*/
/*path：  数据及其所属维度名称*/
/*width： 图表宽度*/
/*height：图标高度*/
/*title： 图标标题*/
/*unit：  数据度量单位*/            
function begin(path, width, height, title, unit, pieCount)
{
    stat_array[0] = new Array();
    stat_array[1] = new Array();

    str = path;
    var strArray = new Array();
    var regExp = /\s+/g;
    var len = 0;

    strArray = str.split(" : ");
    
    for (i=0; i<strArray.length/2; i++)
    {                  
        stat_array[1][i] = strArray[i*2];
        //饼块的数值
        //stat_array[0][i] = parseInt(strArray[i*2+1]);
        stat_array[0][i] = parseFloat(strArray[i*2+1]);
    }
    if (stat_array[0].length > stat_array[1].length)
    {
        len = stat_array[1].length;
    }                           
    else
    {
        len = stat_array[0].length;
    }
    return table(len, 0, 0, width,height,title,unit, pieCount);
}

/* 根据数据绘制所有饼图 */
/* xStrArr:图表标题，及数据所对应的日期*/
/* yValArr:图表数据，二维数组*/
/* titlArr:数据所对应的维度名称，一维数组 */
function drawpie(xStrArr,yValArr,titlArr)
{
    var returnStr = "";
    for(pieCount = 0; pieCount < xStrArr.length; pieCount++)
    {
        var tmStr = "";
        
        for(i = 0; i < yValArr.length - 1; i++)
        {
            tmStr += titlArr[i] + " : " +  yValArr[i][pieCount] + " : ";
        }
        tmStr += titlArr[yValArr.length - 1] + " : " +  yValArr[yValArr.length - 1][pieCount];
        
        var tmStr2 = "" + xStrArr[pieCount];       
        tmStr2 = tmStr2.replace(/(^\s*)|(\s*$)/g, "");  
        returnStr += begin(tmStr, 600, 300, tmStr2, '',pieCount);  
    } 
   
    return returnStr;           
}